//Javascript File for Renrenkong

  var sentences = new Array();
  sentences[0] = "我很诚实";
  sentences[1] = "我为什么要改变以前的计划呢?";
  sentences[2] = "我是个没有决心的人么?";
  
  var is_vow = false;

  // Saves options to localStorage.
  function save_options(){

  var select = document.getElementById("time");
  var time = select.children[select.selectedIndex].value;
  if (!is_vow)	
  if (localStorage.browsingTime < time){
     showTextBox();
     pop_up_questions(time-localStorage.browseTime);
  return;
  }
  
	else
	{	
		send_congratulation_msg();
	}
  
  window.localStorage.clear();
  
  //save the iframe setting
  var e_load_iframe = document.getElementById("load_iframe");
  localStorage.loadiframe = e_load_iframe.value;
  
  //save default URL
  var e_url = document.getElementById("default_url");
  localStorage.defaultURL = e_url.value;


  localStorage.browsingTime = time;
  // Update status to let user know options were saved.
  var status = document.getElementById("status");
  status.innerHTML = "设置保存成功!"; 
  is_vow = false;
  setTimeout(function() {
    status.innerHTML = "";
  }, 750);
  clearGround();
}

  

//claer form
function clearGround(){	
	var boxSection = document.getElementById("copyText");
	boxSection.innerHTML = "";
}

// Show Copy Text Box
function showTextBox(){
	alert("请在文本框内抄写以下句子");
	var html = "";
	var i;
	for (i = 0; i <=2; i++){
		html +=sentences[i] + "<input type=\"text\" id =\"form_"+i+"\" onChange= \"checkCorrectText()\"/><br/>";
	}
	var boxSection = document.getElementById("copyText");
	boxSection.innerHTML = html;
	var btn = document.getElementById("saveBtn");
	btn.disabled = true;
}

function checkCorrectText(){
	var i;
	var element;
	for (i = 0; i <=2; i++){
		element = document.getElementById("form_"+i);
		//console.log(element.value);
		//alert(element.value);
		if (element.value != sentences[i])
			return;
	}
	var btn = document.getElementById("saveBtn");
	btn.disabled = false;
	is_vow = true;
}


// Pop up smart question for the user to answer
function pop_up_questions(){
	// for each miniute added on, user will see a hard question
	alert("ahhh,你把时间延长了!!!");
}


// Send congratulations for downgrade the time barrier
function send_congratulation_msg(){
	alert("没有延长时间，好样的！");
}

// Restores select box state to saved value from localStorage.
function restore_options() {
  var favorite = localStorage.browsingTime ;
  if (!favorite) {
	localStorage.browsingTime = 20; 
  }
  var select = document.getElementById("time");
  for (var i = 0; i < select.children.length; i++) {
    var child = select.children[i];
    if (child.value == favorite) {
      child.selected = "true";
      break;
    }
  }
  var is_iframe_load = localStorage.loadiframe;
  //console.log(is_iframe_load);
  if (is_iframe_load == null){
     is_iframe_load = true;
     localStorage.loadiframe = true;
  }  var e_toggle_btn = document.getElementById("load_iframe");
  e_toggle_btn.value = is_iframe_load;
  setButtonValue(e_toggle_btn.value);

  //get default url
  var url = localStorage.defaultURL;
  if (!url){
     localStorage.defaultURL = "http://google.cn";
     url = localStorage.defaultURL;
  }
  var e_url = document.getElementById("default_url");
  e_url.value = url;
		      
}


function setButtonValue(val){
  var btn = document.getElementById("load_iframe");
  if (val == false || val =="false"){
	btn.value = "false";
        btn.innerHTML = "否";
  }
  else{
        btn.value = "true";
        btn.innerHTML = "是";
      }
}


function toggle_iframe(){
  var btn = document.getElementById("load_iframe");
  if (btn.value == "true"){
	setButtonValue(false);
  }
  else{
        setButtonValue(true);
      }
}

function avoidLoop(){
  var textInput = document.getElementById("default_url");
  var newURL = textInput.value;
  console.log(newURL);
  if (newURL.toLowerCase().indexOf("renren.com") >=0){
	textInput.value = "http://";
        var warningBox = document.getElementById("noRenRenURL");
        warningBox.style.display ="inline-block";
        
        setTimeout(function() { warningBox.style.display = "none";}, 1250);
  }
}
